package com.example.application.controller;

import java.util.*;

import com.example.application.bean.Student;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/students")
public class StudentController {
    // Mock data - replace with database queries later
    private static List<Student> students = new ArrayList<>(Arrays.asList(
            new Student(1, "Alice", 20),
            new Student(2, "Bob", 21),
            new Student(3, "Charlie", 22),
            new Student()
    ));
    // GET /students - get all students
    @GetMapping("")
    public List<Student> getAllStudents() {
        System.out.println("@GetMapping调用");
        return students;
    }
    
    // GET /students/{id} - get a student by id
    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable int id) {
        for (Student s : students) {
            if (s.getId() == id) {
                System.out.println("@GetMapping("+id+")");
                return s;
            }
        }
        return null;  // Return null if student not found
    }
    // POST /students - create a new student
    @PostMapping("")
    public ResponseEntity<String> createStudent(@RequestBody Student student) {
        students.add(student);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    // PUT /students/{id} - update an existing student

    // curl -X PUT -H "Content-Type: application/json" -d '{"id": 3,"name": "MyPUT","age": 20}' http://localhost:8080/students/3
    @PutMapping("/{id}")
    public ResponseEntity<String> updateStudent(@PathVariable int id, @RequestBody Student updatedStudent) {
        System.out.println("@PutMapping("+id+"");
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() == id) {

                students.set(i, updatedStudent);
                return ResponseEntity.status(HttpStatus.OK).build();
            }
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();  // Return 404 if student not found
    }

    // @PutMapping("/{id}")
    // public ResponseEntity<String> updateStudent(@PathVariable int id, @RequestParam Student updatedStudent) {
    //     System.out.println("@PutMapping("+id+"");
    //     for (int i = 0; i < students.size(); i++) {
    //         if (students.get(i).getId() == id) {

    //             students.set(i, updatedStudent);
    //             return ResponseEntity.status(HttpStatus.OK).build();
    //         }
    //     }
    //     return ResponseEntity.status(HttpStatus.NOT_FOUND).build();  // Return 404 if student not found
    // }

    // DELETE /students/{id} - delete a student by id
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteStudentById(@PathVariable int id) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() == id) {
                students.remove(i);
                return ResponseEntity.status(HttpStatus.OK).build();
            }
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();  // Return 404 if student not found
    }
}
